local Utils = {}

function Utils.HookSpawnPrefab(hook_name, new_name, fn, ...)
    local _SpawnPrefab = SpawnPrefab
    function SpawnPrefab(name, skin, skin_id, creator)
        if name == hook_name then
            name = new_name
        end
        return _SpawnPrefab(name, skin, skin_id, creator)
    end

    local result = {fn(...)}

    SpawnPrefab = _SpawnPrefab

    return unpack(result)
end

function Utils.RegisterInventoryItemAtlas(atlas_path)
    local atlas = resolvefilepath(atlas_path)

    local file = io.open(atlas, "r")
    local data = file:read("*all")
    file:close()

    local str = string.gsub(data, "%s+", "")
    local _, _, elements = string.find(str, "<Elements>(.-)</Elements>")

    for s in string.gmatch(elements, "<Element(.-)/>") do
        local _, _, image = string.find(s, "name=\"(.-)\"")
        if image ~= nil then
            RegisterInventoryItemAtlas(atlas, image)
            RegisterInventoryItemAtlas(atlas, hash(image))  -- for client
        end
    end
end

return Utils
